package com.googlecode.androidcartridge;

import org.andromda.metafacades.uml.ModelElementFacade;
import org.apache.commons.lang.StringUtils;

/**
 * 
 * Global utilities. These are made available to templates under the variable
 * ${androidUtils}
 * 
 */
public class AndroidUtils {

	public static boolean findBooleanTaggedValueWithDefault(
			final ModelElementFacade element, final String tagName,
			final boolean defaultValue) {
		Object taggedValue = element.findTaggedValue(tagName);
		boolean result = defaultValue;
		if (taggedValue instanceof String) {
			String str = (String) taggedValue;
			if (StringUtils.isNotBlank(str)) {
				result = Boolean.parseBoolean(str.trim());
			}
		}
		return result;
	}

	/*
	 * The name of the default package that contains the generated files.
	 */
	private String applicationPackage = "org.andromda.android";

	/*
	 * The target Android version. Allowed values are 6,6+api,7,7+api,8,8+api
	 * and so on. Default is 6.
	 */
	private String minSdkVersion;

	/**
	 * Version of Android required in default.properties
	 * 
	 * @return
	 */
	public String getAndroidTargetVersion() {
		String versionString;

		if (StringUtils.isNotBlank(this.minSdkVersion)) {
			if (!this.minSdkVersion.contains("+")) {
				versionString = "android-";
			} else {
				versionString = "Google Inc.:Google APIs:";
			}
			versionString += getAndroidVersion();
		} else {
			versionString = "android-6";
		}
		return versionString;
	}

	/**
	 * An integer describing the minimum android version required to run the
	 * generated program. Referenced in AndroidManifest.xml
	 */
	public int getAndroidVersion() {
		int versionNumber;
		if (StringUtils.isNotBlank(this.minSdkVersion)) {
			String versionString;
			if (this.minSdkVersion.contains("+")) {
				versionString = this.minSdkVersion.split("+")[0];
			} else {
				versionString = this.minSdkVersion;
			}
			versionNumber = Integer.parseInt(versionString);
		} else {
			versionNumber = 6;
		}
		return versionNumber;
	}

	public String getApplicationPackage() {
		return this.applicationPackage;
	}

	public String getMinSdkVersion() {
		return this.minSdkVersion;
	}

	/**
	 * The absolute package name, that is composed by the profile variable
	 * {applicationPackage} and the relative package name passed as argument.
	 * 
	 * @param relativePackage
	 *            the relative package name.
	 * @return the full package name.
	 */
	public String getPackageName(final String localPackage) {
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isNotBlank(this.applicationPackage)) {
			sb.append(this.applicationPackage);
			if (!this.applicationPackage.endsWith(".")) {
				sb.append(".");
			}
		}
		sb.append(localPackage);
		return sb.toString();
	}

	/**
	 * The absolute package path, that is composed by the profile variable
	 * {applicationPackage} and the relative package name passed as argument.
	 * All the packages are separated by a "/".
	 * 
	 * @param relativePackage
	 *            the relative package name.
	 * @return the full package path with packages separated by /
	 */
	public String getPackagePath(final String relativePackage) {
		String packageName1 = getPackageName(relativePackage);
		String packagePath = "/" + packageName1.replace('.', '/');
		return packagePath;
	}

	/**
	 * The name of the default package that contains the generated files.
	 * Injected by cartridge.xml, defined in profile.xml
	 */
	public void setApplicationPackage(final String applicationPackage) {
		this.applicationPackage = applicationPackage;
	}

	/**
	 * The target Android version. Allowed values are 6,6+api,7,7+api,8,8+api
	 * and so on. Default is 6. Injected by cartridge.xml, defined in
	 * profile.xml
	 */
	public void setMinSdkVersion(final String minSdkVersion) {
		this.minSdkVersion = minSdkVersion;
	}

}
